//
//  NYWebViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-7-8.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "NYWebViewController.h"
#import "NSString+HtmlCss.h"

@interface NYWebViewController ()

@property (nonatomic, copy) NSString *htmlString;

@end

@implementation NYWebViewController

@synthesize nyWebView;
@synthesize htmlType;
@synthesize fontSize;
@synthesize htmlString;

- (IBAction)pinchWithGesture:(UIPinchGestureRecognizer *)gesture
{
    if ((gesture.state != UIGestureRecognizerStateEnded))
    {
        return;
    }
    if (gesture.scale < 1.0f)
    {
        // 缩小
        if(self.fontSize == kMinFontSize)
        {
            self.fontSize = kMinFontSize;
        }
        else if(self.fontSize == kMiddleFontSize)
        {
            self.fontSize = kMinFontSize;
        }
        else
        {
            self.fontSize = kMiddleFontSize;
        }
    }
    else
    {
        // 放大
        if(self.fontSize == kMinFontSize)
        {
            self.fontSize = kMiddleFontSize;
        }
        else if(self.fontSize == kMiddleFontSize)
        {
            self.fontSize = kMaxFontSize;
        }
        else
        {
            self.fontSize = kMaxFontSize;
        }
    }
    [self.nyWebView loadHTMLString:self.htmlString baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@app/aboutNoahwm/",kServerAddress]]];
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    if(self.htmlType == kAboutGradeHtml)
    {
        [super leftItemTapped];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.fontSize = kMiddleFontSize;
    NSString *url = nil;
    if(self.htmlType == kAboutGradeHtml)
    {
        url = [NSString stringWithFormat:@"%@%@",kServerAddress,kAboutGradeUrl];
        [self setNaviTitle:@"诺亚会员积分体系"];
        
    }
    else if(self.htmlType == kAboutUshtml)
    {
        url = [NSString stringWithFormat:@"%@%@",kServerAddress,kAboutUsUrl];
        [self setNaviTitle:@"关于诺亚"];
        [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
        [self setRightNaviItemWithTitle:nil imageName:nil];
    }
    else if(self.htmlType == kAboutServiceHtml)
    {
        url = [NSString stringWithFormat:@"%@%@",kServerAddress,kAboutServiceUrl];
        [self setNaviTitle:@"服务条款及保密协议"];
        [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
        [self setRightNaviItemWithTitle:nil imageName:nil];
        [self addSwipePopGesture];
    }
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
        self.view = nil;
        self.nyWebView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    self.nyWebView = nil;
    
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:nil];
    [nyWebView release];
    [htmlString release];
    [super dealloc];
}

#pragma mark - UIWebViewDelegate methdos
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *fs = nil;
    if(self.fontSize == kMinFontSize)
    {
        fs = @"80%";
    }
    else if(self.fontSize == kMiddleFontSize)
    {
        fs = @"100%";
    }
    else
    {
        fs = @"120%";
    }
    NSString *jsString = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@%%'", fs];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSRange startRange = [str rangeOfString:@"<body>"];
    NSRange endRange = [str rangeOfString:@"</body>"];
    
    NSString *bodyString = [str substringWithRange:NSMakeRange(startRange.location+6,endRange.location-startRange.location-6)];
    self.htmlString = [NSString adjustHtmlCSSForString:bodyString];
    [self.nyWebView loadHTMLString:self.htmlString baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@app/aboutNoahwm/",kServerAddress]]];
    
}

- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}

@end
